<template>
	<view class="content">
		<button class="com-btn" @click="getAuth">授权本机号码</button>
		
		
		<!-- <br/><br/>
		<view class="">{{data.aliPhonejd}}</view><br/><br/>
		<!-- <view class="golive"  @click="goUserLive(1)">暂不授权，去往直播间</view> -->
		
		<!-- <br/><br/> -->
		<!-- <view class="">以下是测试反馈数据</view><br/><br/> -->
		<!-- <view class="">1.获取未使用的微信授权code:{{data.code}}</view><br/><br/> -->
		<!-- <button @click="wechatLogin">3.手动触发跳转</button> -->
		<!-- <view class="">1. ali sdk 版本号：{{sdkVersion}}</view><br/><br/> -->
<!-- 		<view class="">2.aliSptoken = {{ data.aliSptoken }}</view>
		<view class="">8.手机号授权={{data.phone}}</view> -->
		<!-- <view class="">6.getAli成功test={{data.test}}</view><br/><br/>
		<view class="">7.getAli失败test2={{data.test2}}</view><br/><br/>
		<view class="">7.test4={{data.test4}}</view><br/><br/>
		<view class="">7.getPhone结果 = {{data.test3}}</view><br/><br/>
		<button @click="copySptoken">复制阿里云返回aliSptoken</button> -->
	</view>
</template>


<script setup>
	import { PhoneNumberServer } from 'aliyun_numberauthsdk_web'; 
	import { getAliToken, getAliPhone, getWxAccess } from '@/api/login.js'
	import { useglobal } from '@/stores'
	
	var phoneNumberServer = new PhoneNumberServer();
	const sdkVersion = phoneNumberServer.getVersion();  // 返回sdk版本号 eg: '1.0.0'
	phoneNumberServer.setLoggerEnable();
	
	const data = reactive({
		shortToken:'',//本站生成短token
		code:'',//微信授权code
		phone:'',//阿里云返回的手机号
		path:'',//跳转路径
		
		showBtn: false,//测试数据
		aliPhonejd: '',
		aliSptoken:'',
		test:'',
		test2:'',
		test3:'',
		test4:''
	})
	
	onLoad(() => {
		// const search = new URLSearchParams(window.location.search)
		// data.code = search.get('code')
	})
	const getAuth = ()=>{
		uni.showLoading({
			title: '加载中'
		})
		getAliToken().then(res=>{
			checkLogin(res.data.accessToken,res.data.jwtToken)
		})
	}
	const checkLogin = (toekn,jwtToken)=> {
		phoneNumberServer.checkLoginAvailable({
			accessToken: toekn,  
			jwtToken: jwtToken,
			success: function (res) {
				if (res.code === 600000) {
					getAliPreToken()
				}else{
					alert(res.message)
				}
				uni.hideLoading();
			},
			error: function (res) {
				alert("登录失败，请关闭wifi,连接移动网络后重试")
				uni.hideLoading()
			}
		});
	}
	const getAliPreToken = ()=> {
		phoneNumberServer.getLoginToken({
			authPageOption: {},
		    success: function (res) {
				// data.test = JSON.stringify(res)//测试可删
				// data.test4 = res.spToken
				getPhone(res.spToken);
		    },
		    // 失败回调
		    error: function (err) {
				// data.test2 = JSON.stringify(err)
				goUserLive('getAliPreToken error')
				// alert("登录失败，请关闭wifi后,连接移动网络后重试")
			},
		});
	}
	const getPhone = (ptoken)=> {
		data.aliSptoken =  ptoken	//测试可删
		getAliPhone({token:ptoken}).then(res=>{
			if(res.code == 230002){
				alert("用户输入手机号错误，请重新认证并填写正确手机号")
			}else{
				data.phone = res.data.phone
				goUserLive(ptoken)
			}
		})
	}
	const goUserLive = (code)=> {
		// data.aliPhonejd = code
		setTimeout(() => {
			wechatLogin()
		}, 500);
	}
	const wechatLogin = ()=> {
		uni.showLoading();
		const search = new URLSearchParams(window.location.search)
		const code = search.get('code')
		const params = {
			code: code,
			company_code: uni.getStorageSync('companyCode'),
			mid: uni.getStorageSync('mid'),
			phone_number: data.phone
		}
		getWxAccess(params).then(res=>{
			uni.hideLoading();
			if(res.code == 200001){
				data.shortToken = res.data.code
				goWeb()
			}else{
				uni.showToast({
					title: res.message,
					icon:'none',
					duration: 2000
				})
			}
		})
	}
	const goWeb = ()=> {
		const companyCode = uni.getStorageSync('companyCode')
		const liveNo = uni.getStorageSync('liveNo')
		const mid = uni.getStorageSync('mid')
		const source = uni.getStorageSync('source')
		const orgin = uni.getStorageSync('orgin')
		
		const shortToken = data.shortToken
		if(source == 'vip'){
			data.path = `${orgin}/pages/live/vip?company_code=${companyCode}&mid=${mid}&code=${shortToken}`
		}else{
			data.path = `${orgin}/pages/live/index?live_no=${liveNo}&company_code=${companyCode}&mid=${mid}&code=${shortToken}`
		}
		window.location.href = data.path
	}
	const copySptoken = ()=> {
		uni.setClipboardData({
			data: data.aliSptoken
		});
	}
</script>

<style lang="scss" scoped>
	.com-btn{margin-top: 30vh;margin-left: 50px;margin-right: 50px;}
	.golive{text-align: center;font-size: 12px;line-height: 45px;color: #d9001b;text-decoration: underline;}
</style>